package user

import (
	"fmt"
	"gin/model"
	"gin/model/database"
	"gin/server"
	"net/http"
	"path"
	"strconv"

	"github.com/gin-gonic/gin"
)

type User struct {
}

// 定义接受数据的结构体
type Login struct {
	User string `form:"user" json:"user" uri:"user" xml:"user" binding:"required"`
	Pwd  string `form:"pwd" json:"pwd" uri:"pwd" xml:"pwd" binding:"required"`
}

func (con User) UserLoginFrom(c *gin.Context) {
	c.HTML(http.StatusOK, "user/userloginform.html", gin.H{})
}

// 导航
func (con User) Table(c *gin.Context) {
	c.HTML(http.StatusOK, "user/table.html", gin.H{})

}

// 首页
func (con User) Index(c *gin.Context) {
	c.HTML(http.StatusOK, "user/index.html", gin.H{})

}

// 登录请求处理
func (con User) UserLoginData(c *gin.Context) {
	userdata := &model.User{
		Id:         0,
		UserName:   "",
		User:       "",
		Phone:      "",
		Pwd:        "",
		UserImg:    "",
		UserState:  0,
		TargetId:   0,
		TargetType: "",
	}
	var form Login
	if err := c.Bind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	where := map[string]any{
		"user": form.User,
	}
	database.DB.Table("user").Where(where).Find(userdata)
	if form.User != userdata.User {
		server.Return(c, 201, "账号不正确", nil)
		return
	}
	if form.Pwd != userdata.Pwd {
		server.Return(c, 202, "密码不正确", nil)
		return
	}
	//设置cookie
	server.Setcookie(c)
	server.Return(c, 200, "登录成功", nil)

}

// 用户添加表单
func (con User) UserAddFrom(c *gin.Context) {
	c.HTML(http.StatusOK, "user/useraddform.html", gin.H{})
}

// 单文件上传
func (con User) UserAddFromSum(c *gin.Context) {
	name := c.PostForm("name")
	img1, err1 := c.FormFile("img")
	var dst string
	if err1 == nil {
		dst := path.Join("./static/upload", img1.Filename)
		_ = c.SaveUploadedFile(img1, dst)
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"name":    name,
		"dst":     dst,
	})
}

// 相同名字多文件上传文件上传
func (con User) UserAddFromSums(c *gin.Context) {
	name := c.PostForm("name")
	form, _ := c.MultipartForm()
	files := form.File["img[]"]
	var imgurl []string
	for _, file := range files {
		dst := path.Join("./static/upload", file.Filename)
		c.SaveUploadedFile(file, dst)
		imgurl = append(imgurl, dst)
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"name":    name,
		"dst":     imgurl,
	})
}

// 登出
func (con User) LoginOut(c *gin.Context) {
	fmt.Println(1111111111111)
	cookie := http.Cookie{
		Name:   "session_id",
		Value:  "",
		Path:   "/",
		MaxAge: -1,
	}
	//设置cookie
	http.SetCookie(c.Writer, &cookie)
	server.Return(c, 200, "登出成功", nil)
}

// 用户列表
func (con User) UserList(c *gin.Context) {
	var users []*model.User
	if err := database.DB.Table("user").Find(&users).Error; err != nil {
		c.JSON(500, gin.H{
			"error": err,
		})
		return
	}
	for _, v := range users {
		v.UserImg = server.BaseURL + v.UserImg

	}
	server.LogError("kjhdbjsadhh%v", "我的头发是真发")
	c.HTML(http.StatusOK, "user/userList.html", gin.H{
		"data": users,
	})
	//c.JSON(200, gin.H{
	//	"users": users,
	//})

}
func (con User) UserAdd(c *gin.Context) {
	m := &model.User{
		UserName: "",
		User:     "",
		Phone:    "",
		Pwd:      "",
		UserImg:  "",
	}
	m.UserName = c.PostForm("user_name")
	m.User = c.PostForm("name")
	m.Phone = c.PostForm("phone")
	m.Pwd = c.PostForm("pwd")
	file, err := c.FormFile("user_img")
	if err != nil {
		server.Success(c, "添加失败", err)
	}
	if m.UserName == "" || m.User == "" || m.Phone == "" || m.Pwd == "" {
		c.JSON(200, gin.H{
			"code": 301,
			"msg":  "user_name,name,phone,pwd,user_img参数不能为空",
			"data": nil,
		})
	}
	m.UserImg = server.FileUpload(file, c)
	err = database.DB.Table("user").Save(&m).Error
	if err != nil {
		server.Success(c, "添加失败", err)
		return
	}
	server.Success(c, "添加成功", nil)
}
func (con User) UserDel(c *gin.Context) {
	m := &model.User{
		Id: 0,
	}
	m.Id, _ = strconv.Atoi(c.PostForm("id"))
	if m.Id == 0 {
		c.JSON(200, gin.H{
			"code": 301,
			"msg":  "删除ID不能为空",
			"data": nil,
		})
	}
	err := database.DB.Table("user").Delete(&m).Error
	if err != nil {
		server.Success(c, "删除失败", err)
		return
	}
	server.Success(c, "删除成功", nil)
}

// 修改
func (con User) UserUpd(c *gin.Context) {
	data := make(map[string]any)
	user_name := c.PostForm("user_name")
	user := c.PostForm("user")
	phone := c.PostForm("phone")
	pwd := c.PostForm("pwd")
	id := c.PostForm("id")
	if user_name != "" {
		data["user_name"] = user_name
	}
	if user != "" {
		data["user"] = user
	}
	if phone != "" {
		data["phone"] = phone
	}
	if pwd != "" {
		data["pwd"] = pwd
	}
	if id == "" {
		server.Success(c, "更新ID不能为空", nil)
	}
	//定义查询条件
	User := &model.User{
		Id: 0,
	}
	User.Id, _ = strconv.Atoi(id)
	file, _ := c.FormFile("user_img")
	if file != nil {
		data["user_img"] = server.FileUpload(file, c)
	}
	//批量修改
	err := database.DB.Model(&User).Updates(data).Error
	if err != nil {
		server.Success(c, "更新失败", err)
		return
	}
	server.Success(c, "更新成功", nil)
}
